package longrise.phone.com.bjjt_jyb.Utils;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.ImageView;


import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;

import longrise.phone.com.bjjt_jyb.R;

public class ImageLoaderForSettingUtils
{

	private static ImageLoaderForSettingUtils instance;

	private ImageLoaderConfiguration config;

	private DisplayImageOptions options;

	private Context context;

	private ImageLoaderForSettingUtils(Context context)
	{
		this.context = context;
	}

	public static ImageLoaderForSettingUtils getInstance(Context context)
	{
		if (instance == null)
		{
			instance = new ImageLoaderForSettingUtils(context);
		}
		return instance;
	}

	public void setConfig(ImageLoaderConfiguration config)
	{
		this.config = config;
	}

	public void setOptions(DisplayImageOptions options)
	{
		this.options = options;
	}

	public void display(String url, ImageView imageView)
	{
		if (config == null)
		{
			config = initDefaultConig();
		}
		ImageLoader.getInstance().init(config);
		if (options == null)
		{
			options = initDefaultOptions();
		}
		ImageLoader.getInstance().displayImage(url, imageView, options);
	}

	public void display(String url, ImageView imageView, ImageLoadingListener listener)
	{
		if (config == null)
		{
			config = initDefaultConig();
		}
		ImageLoader.getInstance().init(config);
		if (options == null)
		{
			options = initDefaultOptions();
		}
		ImageLoader.getInstance().displayImage(url, imageView, options, listener);
	}

	public void display(String url, ImageView imageView, ImageLoadingListener listener,ImageLoadingProgressListener progressListener)
	{
		if (config == null)
		{
			config = initDefaultConig();
		}
		ImageLoader.getInstance().init(config);
		if (options == null)
		{
			options = initDefaultOptions2();
		}
		ImageLoader.getInstance().displayImage(url, imageView, options, listener,progressListener);
	}

	/**
	 * 从内存卡中异步加载本地图片
	 *
	 * @param uri
	 * @param imageView
	 */
	public void displayFromSDCardAddListener(String uri, ImageView imageView,ImageLoadingListener listener) {
		// String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
		if (config == null)
		{
			config = initDefaultConig();
		}
		ImageLoader.getInstance().init(config);
		if (options == null)
		{
			options = initDefaultOptions();
		}
		ImageLoader.getInstance().displayImage("file://" + uri, imageView,options,listener);
	}

	/**
	 * 从内存卡中异步加载本地图片
	 *
	 * @param uri
	 * @param imageView
	 */
	public void displayFromSDCard(String uri, ImageView imageView) {
		// String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
		if (config == null)
		{
			config = initDefaultConig();
		}
		ImageLoader.getInstance().init(config);
		if (options == null)
		{
			options = initDefaultOptions();
		}
		ImageLoader.getInstance().displayImage("file://" + uri, imageView,options);
	}

	private ImageLoaderConfiguration initDefaultConig()
	{
		String path = getCacheFilePath(null);
		File file = new File(path).getParentFile();
		if (!file.exists())
		{
			file.mkdirs();
		}
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPoolSize(3)
				.threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
				.memoryCache(new WeakMemoryCache()).diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
				.diskCache(new UnlimitedDiskCache(file)).build();

		return config;
	}
	

	/**
	 * 获取下载文件的本地路径
	 */
	public String getCacheFilePath(String fileSuffix) {
		if (!isSDExits()) {
			return null;
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append(Environment.getExternalStorageDirectory());
		buffer.append("/");
		buffer.append("chatdemo");
		buffer.append("/");
		buffer.append("cache");
		buffer.append("/");
		buffer.append(System.currentTimeMillis());
		if (!TextUtils.isEmpty(fileSuffix)) {
			buffer.append(fileSuffix);
		}

		return buffer.toString();
	}
	
	public boolean isSDExits() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		}
		return false;
	}

	private DisplayImageOptions initDefaultOptions()
	{
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.icon07)
				.showImageOnFail(R.drawable.icon07).cacheInMemory(false).cacheOnDisk(true)
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED).build();

		return options;
	}

	private DisplayImageOptions initDefaultOptions2()
	{
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.icon07)
				.showImageOnFail(R.drawable.icon07).cacheInMemory(true).cacheOnDisk(true)
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED).build();

		return options;
	}
	public void clearCache(){
		ImageLoader.getInstance().clearMemoryCache();
		ImageLoader.getInstance().clearDiskCache();
	}
}
